//
//  MineViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/22.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "MineViewController.h"
#import "MineDetailViewController.h"
#import "LoginViewController.h"
@interface MineViewController () <UITableViewDataSource, UITableViewDelegate,UIAlertViewDelegate>// 签订协议
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSString *nameStr;
@property (nonatomic, strong) NSString *fileStr;
@property (nonatomic, strong) NSString *jumdge;
@end

@implementation MineViewController

// 页面将要出现的时候

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    BOOL a = [[NSUserDefaults standardUserDefaults] boolForKey:@"didLogin"];
    _button.selected = a;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableview];
    
    _tableview.dataSource = self;
    _tableview.delegate = self;
    
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    NSArray *arr = @[@"我的活动", @"我的电影",@"清除缓存"];
    self.arr = [arr mutableCopy];
    // 右侧登陆按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setTitle:@"登陆" forState:UIControlStateNormal];
    [_button setTitle:@"注销" forState:UIControlStateSelected];
    _button.frame = CGRectMake(0, 0, 50, 50);
    
    //将按钮的状态保存在单例里面
    [[NSUserDefaults standardUserDefaults] setBool:_button.selected forKey:@"button"];
    
    [_button addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_button];
    // navigation 设置
    self.navigationItem.title = @"我的";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:UIBarMetricsDefault];
    
    BOOL a = [[NSUserDefaults standardUserDefaults] boolForKey:@"didLogin"];
    _button.selected = a;


}

///右侧登陆按钮方法实现

-(void)login:(UIButton *)btn
{
    //判断当前的状态是不是登陆 根据状态要显示按钮是登陆还是注销 以及 弹出的是登陆页面还是提示注销
    BOOL a = [[NSUserDefaults standardUserDefaults] objectForKey:@"didLogin"];
    if (a) {
        _button.selected = YES;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否注销?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // 改变右侧状态
            // 将状态改为注销并且注销掉
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user removeObjectForKey:@"didLogin"];
            //            [user removeObjectForKey:@"didLike"];
            _button.selected = !_button.selected;
//            _button.titleLabel.text = @"注销";
            
        }];
        [alert addAction:cancelAction];
        [alert addAction:otherAction];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];

        
    }else
    {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *loginNavi = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginVC.navigationItem.title = @"用户登陆";
            btn.selected = !btn.selected;
        [self.navigationController presentViewController:loginNavi animated:YES completion:^{
            
        }];
    }
    
//    LoginViewController *loginVC = [[LoginViewController alloc] init];
//    UINavigationController *loginNavi = [[UINavigationController alloc] initWithRootViewController:loginVC];
//    loginVC.navigationItem.title = @"用户登陆";
////    btn.selected = !btn.selected;
//    [self.navigationController presentViewController:loginNavi animated:YES completion:^{
//        
//    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = _arr[indexPath.row];
    return cell;
}

///点击cell进入详情页

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    // 进入登陆界面
    if (indexPath.row != 2) {
        // 判断当前状态是不是已经登陆
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        BOOL a = [user objectForKey:@"didLogin"];
        if (a) {
            // 进入详情界面
            NSLog(@"详情页面");
            MineDetailViewController *mineVC = [[MineDetailViewController alloc] init];
            // 判断是我的活动 或者是 我的电影
            if (indexPath.row == 0) {
                self.nameStr = @"activity.plist";
                self.fileStr = @"activity";
                self.jumdge = @"0";
                mineVC.Juge = _jumdge;
                
            }else
            {
                self.nameStr = @"movie.plist";
                self.fileStr = @"movie";
                self.jumdge = @"1";
                mineVC.Juge = _jumdge;
            }
            // 将路径后缀传递到下一页
            mineVC.nameStr = _nameStr;
            mineVC.fileStr = _fileStr;
            [self.navigationController pushViewController:mineVC animated:YES];
            
            
        }else
        {
        
    
       
            // 进入登陆界面
            NSLog(@"登陆页面");
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
            //    loginVC.navigationController.title = @"";
            [self.navigationController presentViewController:navi animated:YES completion:^{
                
            }];

        }
        
        
    }else
    {
        // 提示是否注销
        UIAlertController *art = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // 改变右侧状态
            // 将状态改为登陆并且注销掉
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user removeObjectForKey:@"didLogin"];
            //将当前的用户名移除掉
            [user removeObjectForKey:@"userName"];
//            [user removeObjectForKey:@"didLike"];
            _button.selected = !_button.selected;
        }];
        [art addAction:cancelAction];
        [art addAction:otherAction];
        
        [self presentViewController:art animated:YES completion:^{
            
        }];
        
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
